﻿using System.Collections.Generic;
using System.Linq;
using HVACWebService.Common;
using HVACWebService.DomainModel.WeiXin;

namespace HVACWebService.Cloud.WeChat
{
    public class WeiXinUser
    {
        private readonly string _getWeiXinUserUrl = ConfigHelper.GetValue<string>("GetWeiXinUserUrl");
        private readonly string _getWeiXinUserUrls = ConfigHelper.GetValue<string>("GetWeiXinUserUrls");



        public WeiXinUserInfoExtendAbleAndDataBase GetUserInfo(string accessToken, string openId)
        {
            var httpWebRequestHelp = new HttpWebRequestHelp();
            var url = string.Format(_getWeiXinUserUrl, accessToken, openId);
            var getData = httpWebRequestHelp.GetDataStringUTF_8(url);
            return JsonSerializer.Instance.Deserialize<WeiXinUserInfoExtendAbleAndDataBase>(getData);
        }


        public GetUserList GetUserInfos(string accessToken, PostUserList postUserList)
        {
            var getUserList = new GetUserList { user_info_list = new List<WeiXinUserInfoExtendAbleAndDataBase>() };
            var httpWebRequestHelp = new HttpWebRequestHelp();
            var url = string.Format(_getWeiXinUserUrls, accessToken);
            var getWeiXinUserNumber = ConfigHelper.GetValue<int>("GetWeiXinUserNumber");
            var userCount = postUserList.user_list.Count;
            var totalPages = userCount % getWeiXinUserNumber == 0 ? userCount / getWeiXinUserNumber : userCount / getWeiXinUserNumber + 1;
            for (int i = 0; i < totalPages; i++)
            {
                var postUser = new PostUserList { user_list = new List<PostUserData>() };
                postUser.user_list = postUserList.user_list.Take((i + 1) * getWeiXinUserNumber).Skip(getWeiXinUserNumber * (i)).ToList();
                var postUserStr = JsonSerializer.Instance.Serialize(postUser);
                var getData = httpWebRequestHelp.PostDataStringUTF_8(url, postUserStr);
                var returnWeChatUser = JsonSerializer.Instance.Deserialize<GetUserList>(getData);
                if (returnWeChatUser?.user_info_list != null)
                {
                    getUserList.user_info_list.AddRange(returnWeChatUser.user_info_list);
                }
                else
                {
                    getUserList.user_info_list.AddRange(GetOneUserInfo(accessToken, postUser, url).user_info_list);
                }
            }
            return getUserList;
        }

        public GetUserList GetOneUserInfo(string accessToken, PostUserList postUserList,string url)
        {

            var getUserList = new GetUserList { user_info_list = new List<WeiXinUserInfoExtendAbleAndDataBase>() };
            var httpWebRequestHelp = new HttpWebRequestHelp();
            foreach (var s in postUserList.user_list)
            {
                var postUser = new PostUserList { user_list = new List<PostUserData>() };
                postUser.user_list.Add(s);
                var postUserStr = JsonSerializer.Instance.Serialize(postUser);
                var getData = httpWebRequestHelp.PostDataStringUTF_8(url, postUserStr);
                var returnWeChatUser = JsonSerializer.Instance.Deserialize<GetUserList>(getData);
                if (returnWeChatUser?.user_info_list != null)
                {
                    getUserList.user_info_list.AddRange(returnWeChatUser.user_info_list);
                }
            }


            return getUserList;
        }
    }
}
